<?php
/** @noinspection ALL */
declare (strict_types=1);

namespace app\mall\service\product\admin;

use app\common\logic\category\admin\Category;
use app\common\service\BaseService;
use app\mall\logic\product\admin\Product as ProductLogic;
use eduline\admin\libs\pageform\FormItem;
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageForm;
use eduline\admin\page\PageList;
use think\annotation\Inject;
use think\facade\Request;
use think\Response;

/**
 * 机构管理-后台
 */
class Product extends BaseService
{
    /**
     * @Inject()
     * @var ProductLogic
     */
    protected $prodLogic;

    /**
     * 商品列表
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 22/11/25
     *
     * @return mixed
     */
    public function list()
    {
        $map = [];
        if (input('get.is_recommend') && input('get.is_recommend/d', -1) !== -1) $map[] = ['is_recommend', '=', 1];
        if (input('?get.title')) $map[] = ['title', 'like', '%' . input('get.title/s') . '%'];
        if (input('?get.category')) $map[] = ['category', 'like', '%,' . input('get.category/d') . ',%'];
        if (input('?get.status') && input('get.status/d', -1) !== -1) $map[] = ['status', '=', input('get.status/d')];

        $data = $this->prodLogic->getList($map);
        // 定义字段
        $keyList = [
            'cover_url'    => ListItem::make('custom')->title('封面'),
            'title'        => ListItem::make()->title('商品'),
            'category'     => ListItem::make()->title('分类'),
            'cash_price'   => ListItem::make()->title('现金价格'),
            'credit_price' => ListItem::make()->title('积分抵扣'),
            'sale_count'   => ListItem::make()->title('销量'),
            'stock_num'    => ListItem::make()->title('库存数量'),
            'carriage'     => ListItem::make()->title('运费'),
            'is_recommend' => ListItem::make('custom')->title('推荐'),
            'status'       => ListItem::make('custom')->title('上架状态'),
        ];
        // 机构分类
        $category  = app(Category::class)->cascaderCate(0, 6);
        $searchKey = [
            'is_recommend' => FormItem::make('select')->title(false)->placeholder('所有/推荐')->options([
                ['title' => '所有', 'value' => -1],
                ['title' => '推荐', 'value' => 1],
            ]),
            'title'        => FormItem::make()->title(false)->placeholder('名称')->maxlength(30),
            'category'     => FormItem::make('cascader')->title(false)->options($category)->placeholder('分类')->required(),
            'status'       => FormItem::make('select')->title(false)->placeholder('状态')->options([
                ['title' => '不限制', 'value' => -1],
                ['title' => '正常', 'value' => 1],
                ['title' => '禁止', 'value' => 0],
            ]),
        ];
        // 设置表单
        $list = app(PageList::class);
        // 表单字段
        $list->pageKey   = $keyList;
        $list->searchKey = $searchKey;
        $list->datas     = $data;

        return $list->send();
    }

    /**
     * 创建商品
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 22/11/25
     *
     * @return Response
     */
    public function create(): Response
    {
        if (Request::isPost()) {
            $data = Request::post();
            $this->prodLogic->createProduct($data);

            return $this->parseToData([], 1, '新建成功');
        }
        /* 创建课程界面配置 */
        $keyList = $this->keyListInit('create');
        // 设置表单
        $form = app(PageForm::class);
        // 表单字段
        $form->pageKey = $keyList;

        $form->saveMethod  = 'POST';
        $form->savePostUrl = '/mall/admin/product/create';

        return $form->send();

    }

    /**
     * 编辑商品
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 22/11/25
     *
     * @param int $id
     * @return Response
     */
    public function edit(int $id): Response
    {
        if (Request::isPut()) {
            $param = Request::put();
            $this->prodLogic->editProduct($id, $param);

            return $this->parseToData([], 1, '编辑成功');
        }

        $data         = $this->prodLogic->with(['cover_url', 'swiper_list'])->findOrFail($id);
        $data->swiper = $data->swiper_list ? array_column($data->swiper_list->toArray(), 'attach_id') : [];
        $data->cates  = $data->fullcategorypath;
        // 创建课程界面配置
        $keyList = $this->keyListInit('edit');
        // 设置表单
        $form = app(PageForm::class);
        // 表单字段
        $form->datas       = $data;
        $form->pageKey     = $keyList;
        $form->saveMethod  = 'PUT';
        $form->savePostUrl = '/mall/admin/product/' . $id . '/edit';

        return $form->send();
    }

    /**
     * 界面配置
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 22/11/25
     *
     * @return array
     */
    public function keyListInit($action): array
    {
        // 机构分类
        $category = app(Category::class)->cascaderCate(0, 6);

        // 定义字段
        return [
            'title'          => FormItem::make()->title('名称')->required()->maxlength(30),
            'cates'          => FormItem::make('cascader')->title('分类')->options($category)->required(),//->multiple(true)
            'cover'          => FormItem::make('custom')->title('封面')->required()->help('建议宽高尺寸为220px*220px'),
            'swiper'         => FormItem::make('custom')->title('轮播图')->required()->help('建议宽高尺寸为400px*400px'),
            'info'           => FormItem::make('editor')->title('详细介绍')->required(),
            'scribing_price' => FormItem::make('inputNumber')->title('划线价格')->min(0)->precision(2)->required(),
            'cash_price'     => FormItem::make('inputNumber')->title('销售价(现金)')->min(0)->precision(2)->required()->help('为0时全部用积分支付'),
            'credit_price'   => FormItem::make('inputNumber')->title('销售价(积分)')->min(0)->required()->help('为0时全部现金支付'),
            'carriage'       => FormItem::make('inputNumber')->title('运费')->min(0)->precision(2),
            'stock_num'      => FormItem::make('inputNumber')->title('库存')->required(),
            // 'is_recommend'   => FormItem::make('slideSwitch')->title('推荐')->activeValue()->inactiveValue(0),
            // 'best_sort'      => FormItem::make('inputNumber')->title('排序值'),
            'fictitious'     => FormItem::make('slideSwitch')->title('虚拟商品')->activeValue()->inactiveValue(0)->visible($action != 'edit'),
            // 'status'         => FormItem::make('slideSwitch')->title('状态')->activeValue()->inactiveValue(0),
        ];
    }

    /**
     * 编辑字段
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 22/11/28
     *
     * @param int $id
     * @param     $field
     * @return Response
     */
    public function editField(int $id, $field): Response
    {
        $value  = Request::put($field);
        $school = $this->prodLogic->editField($id, $field, $value);

        return $this->parseToData($school, 1, '更新成功');
    }

    /**
     * 删除商品
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 22/11/28
     *
     * @param int $id
     * @return Response
     */
    public function delete(int $id): Response
    {
        $this->prodLogic->deleteProduct($id);

        return $this->parseToData([], 1, '删除成功');
    }

    /**
     * 批量下架
     *
     * @return Response
     */
    public function disables()
    {
        $ids = Request::put('ids/a');
        $this->prodLogic->batchUpdated($ids, 'status', 0);

        return $this->parseToData([], 1, '操作成功');
    }

    /**
     * 批量上架
     *
     * @return Response
     */
    public function enables()
    {
        $ids = Request::put('ids/a');
        $this->prodLogic->batchUpdated($ids, 'status', 1);

        return $this->parseToData([], 1, '操作成功');
    }

    /**
     * 批量删除
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 22/11/28
     *
     * @param $ids
     * @return Response
     */
    public function deletes($ids): Response
    {
        $this->prodLogic->deleteProducts($ids);

        return $this->parseToData([], 1, '操作成功');
    }
}
